package org.example.service;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(!username.equals("jason")){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        String pass = bCryptPasswordEncoder.encode("123456");
        //这里要注意一个问题，使用这个AuthorityUtils.commaSeparatedStringToAuthorityList需要前面添加ROLE_XXXX
        return new User(username,pass, AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_admin"));
    }
}
